package com.prueba.server.persistencia.rol;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Set;
import java.util.Vector;

import javax.servlet.ServletContext;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class ParserRoles {

	// Etiquetas XML :
	private static final String TAG_ROL = "Rol";
	private static final String TAG_TIPO = "TipoVista";

	private static final Hashtable<String, Rol> rolesHash = new Hashtable<String, Rol>();

	public static void init(ServletContext context) {
		Document doc = null;
		
		InputStream rolesXML = context.getResourceAsStream("/WEB-INF/Roles.xml");
		
		
	
		
		
		try {
			// Create a builder factory
			DocumentBuilderFactory factory = DocumentBuilderFactory
					.newInstance();

			// Create the builder and parse the file
			doc = factory.newDocumentBuilder().parse( rolesXML
					);
			

			
		
		} catch (SAXException e) {

			System.out.println(e.getMessage());
			
		} catch (ParserConfigurationException e) {

			System.out.println(e.getMessage());
			
		} catch (IOException e) {
			
			System.out.println("Error leyendo Roles.xml");

		} 
		

		Element root = doc.getDocumentElement();

		NodeList children = root.getElementsByTagName(TAG_ROL);

		for (int i = 0; i < children.getLength(); i++) {

			ArrayList<NivelDeseadoStakeholder> nivelesDeseados = new ArrayList<NivelDeseadoStakeholder>();

			Rol rol;

			Element rolElement = (Element) children.item(i);

			NodeList tipos = rolElement.getElementsByTagName(TAG_TIPO);

			for (int j = 0; j < tipos.getLength(); j++) {

				Element tipo = (Element) tipos.item(j);
				System.out.println("LEVANTANDO NIVEL DESEADO: ");
				System.out.println(tipo.getAttribute("nivelDeseado"));
				nivelesDeseados.add(new NivelDeseadoStakeholder(tipo.getAttribute("id"),
						Integer.parseInt(tipo.getAttribute("nivelDeseado"))));

			}

			rol = new Rol(rolElement.getAttribute("id"), nivelesDeseados);

			rolesHash.put(rol.getNombre(), rol);

		}
	}

	public static String[] getRoles() {

		Set<String> roles = rolesHash.keySet();
		String resultado[] = new String[rolesHash.size()];
		int posicion=0;
		for (String r : roles) {
			
			resultado[posicion] = r;
			posicion++;

		}
		
		
		
		return resultado;
	}

	public static ArrayList<Rol> getRoles(String roles) throws RuntimeException {

		String[] elementos = roles.split(",");

		ArrayList<Rol> resultado = new ArrayList<Rol>();

		for (int i = 0; i < elementos.length; i++) {

			Rol auxRol = rolesHash.get(elementos[i]);

			if (auxRol == null) {
				throw new RuntimeException("Error: No se encontro el rol: "
						+ elementos[i]);
			} else {
				resultado.add(auxRol);
			}

		}

		return resultado;

	}

}